package com.mes.controller.quality;

import com.mes.entity.User;
import com.mes.service.quality.ProductTestService;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("productTest")
public class ProductTestController extends BaseCtrl {
	@Autowired
	private ProductTestService productTestService;

	//根据传过来的RFID带出明细信息
    @ResponseBody
    @RequestMapping(value="getproductTestLists", method=RequestMethod.POST)
    public Message getproductTestLists(HttpServletRequest request) {
        Message msg = new Message();

        String code = request.getParameter("rfid").toString();   //rifd

        //判断该工单是否已完成入库及上道工序完成状态及工单
        List<Map<String, Object>> zyList = productTestService.getPhotoLists(code);
        String zygxStatus = "";
        if (zyList.size() > 0) {
            //判断上道工序执行状态
            zygxStatus = zyList.get(0).get("ZYGXSTATUS").toString();

            if (!zygxStatus.equals("FINISHED")) {
                msg.setData("results", "false");
                msg.setErrorMessage("上道工序未完工！");
                return msg;
            }
        } else {
            msg.setData("results", "false");
            msg.setErrorMessage("[" + code + "]不存在！");
            return msg;
        }
        msg.setData("rfidLists",productTestService.getPhotoLists(code));

        return msg;
    }

    //判断该工单合格
    @ResponseBody
    @RequestMapping(value="getChecjResult", method=RequestMethod.POST)
    public Message getChecjResult(HttpServletRequest request) {
        Message msg = new Message();

        String code = request.getParameter("rfid").toString();   //rifd

        List<Map<String, Object>> rfidLists = productTestService.getPhotoLists(code);
        User user = UserUtil.getUser();

        if(rfidLists.size() > 0){
            String PRODUCTID = rfidLists.get(0).get("PRODUCTID") == null ? null : rfidLists.get(0).get("PRODUCTID").toString();

            String PROCESSID = rfidLists.get(0).get("PROCESSID") == null ? null : rfidLists.get(0).get("PROCESSID").toString();

            String ZYID = rfidLists.get(0).get("ZYID") == null ? null : rfidLists.get(0).get("ZYID").toString();

            String GROUPID = rfidLists.get(0).get("GROUPID") == null ? null : rfidLists.get(0).get("GROUPID").toString();
            String ITEMID = rfidLists.get(0).get("ITEMID") == null ? null : rfidLists.get(0).get("ITEMID").toString();
            String XMNAME = rfidLists.get(0).get("XMNAME") == null ? null : rfidLists.get(0).get("XMNAME").toString();
            String EXEID = rfidLists.get(0).get("EXEID") == null ? null : rfidLists.get(0).get("EXEID").toString();
            String joStatus = (String)rfidLists.get(0).get("JO_STATUS");
            if(StringUtils.isNotEmpty(joStatus) && !"CREATE".equals(joStatus)) {
            	msg.setData("results", "false");
            	msg.setErrorMessage("工单：" + code + " 已检！");
            	return msg;
            }
            String status = "QUALIFIED";
            String results = "QUALIFIED";
            String resultId = productTestService.inQualityResult(PRODUCTID,PROCESSID, status,  results, ZYID, user,  GROUPID);

            for(int i = 0 ; i <rfidLists.size() ; i++ ){
                productTestService.inQualityResultValue(resultId, ITEMID, user, XMNAME);
            }
            String ending = "FINISHED";
            productTestService.UpdateExeEndStatus(EXEID,ending);
            msg.setData("results", "true");
        } else {
            msg.setData("results", "false");
            msg.setErrorMessage("[" + code + "]不存在！");
            return msg;
        }

        return msg;
    }

    //判断该工单不合格
    @ResponseBody
    @RequestMapping(value="getNoResult", method=RequestMethod.POST)
    public Message getNoResult(HttpServletRequest request) {
        Message msg = new Message();

        String code = request.getParameter("rfid").toString();   //rifd
        String nopassItemIds = request.getParameter("nopassItemIds");	//不合格项

        List<Map<String, Object>> rfidLists = productTestService.getPhotoLists(code);
        User user = UserUtil.getUser();

        if(rfidLists.size() > 0){
            String PRODUCTID = rfidLists.get(0).get("PRODUCTID") == null ? null : rfidLists.get(0).get("PRODUCTID").toString();

            String PROCESSID = rfidLists.get(0).get("PROCESSID") == null ? null : rfidLists.get(0).get("PROCESSID").toString();

            String ZYID = rfidLists.get(0).get("ZYID") == null ? null : rfidLists.get(0).get("ZYID").toString();

            String GROUPID = rfidLists.get(0).get("GROUPID") == null ? null : rfidLists.get(0).get("GROUPID").toString();
//            String ITEMID = rfidLists.get(0).get("ITEMID") == null ? null : rfidLists.get(0).get("ITEMID").toString();
//            String XMNAME = rfidLists.get(0).get("XMNAME") == null ? null : rfidLists.get(0).get("XMNAME").toString();
            String EXEID = rfidLists.get(0).get("EXEID") == null ? null : rfidLists.get(0).get("EXEID").toString();
            String joStatus = (String)rfidLists.get(0).get("JO_STATUS");
            if(StringUtils.isNotEmpty(joStatus) && !"CREATE".equals(joStatus)) {
            	msg.setData("results", "false");
            	msg.setErrorMessage("工单：" + code + " 已检！");
            	return msg;
            }
            String status = "UNQUALIFIED";
            String results = "UNQUALIFIED";
            String resultId = productTestService.inQualityResult(PRODUCTID,PROCESSID, status,  results, ZYID, user,  GROUPID);

            if(nopassItemIds.length() > 0) {
        		String[] itemIdArr = nopassItemIds.split(",");
        		for (String itemId : itemIdArr) {
					for (Map<String, Object> item : rfidLists) {
						if(itemId != null && itemId.equals(item.get("ITEMID"))) {
							String xmName = (String)item.get("XMNAME");
							productTestService.inQualityResultValue(resultId, itemId, user, xmName);
							continue;
						}
					}
				}
        	}
            String ending = "NO";
            productTestService.UpdateExeEndStatus(EXEID,ending);
            msg.setData("results", "true");
        } else {
            msg.setData("results", "false");
            msg.setErrorMessage("[" + code + "]不存在！");
            return msg;
        }

        return msg;
    }

}